// 封装分类处理逻辑的业务代码
import { getCategory } from '@/apis/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { ref, onMounted } from 'vue'

export const useCategory = () => {
  const route = useRoute()
  // 获取分类列表数据
  const categoryData = ref({})
  const getCategoryList = async (id) => {
    const { result } = await getCategory(id)
    categoryData.value = result
  }
  onMounted(() => {
    getCategoryList(route.params.id)
  })
  // 路由更新时重新获取数据,精确更新
  onBeforeRouteUpdate((to) => {
    // console.log(to)
    getCategoryList(to.params.id)
  })
  return { 
    categoryData
  }
}